/*
  The MIT License
  
  Copyright (c) 2010 smath-view project
  
  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:
  
  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.
  
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE.
*/
package pl.smath.view.model;

import pl.smath.view.renderer.TermRendererException;
import pl.smath.view.utils.Cursor;
import pl.smath.view.utils.Rectangle;
import gwt.g2d.client.graphics.Surface;

/**
 * FractionView - represents rendered fraction.
 * 
 * @author Damian Rodziewicz
 *
 */
public class FractionView extends View {
	private final View numerator;
	private final View denominator;
	
	private final static Double space = 2d;
	
	/**
	 * @return the space
	 */
	public static Double getSpace() {
		return space;
	}

	/**
	 * Constructor
	 */
	public FractionView(View numerator, View denominator) {
		super("/");
		this.numerator = numerator;
		this.denominator = denominator;
	}
	
	/**
	 * render text at the current position
	 * @param canvas
	 */
	@Override
	public void render(Surface canvas) throws TermRendererException {
		//TODO brackets
		Surface surface = new Surface();
		surface.setFont(this.getFont());
		Double minusWidth = this.minusSign.getWidth();
		Cursor cursor = this.getCursor();
		Rectangle rectangle = this.getRectangle();
		
		if ((this.getFont() == null)||(this.getFontHeight() == null)) {
			this.setFont(canvas.getFont());
		}
		canvas.setFont(this.getFont());
		
		if (isNegative()) {
			this.minusSign.render(canvas, new Cursor(cursor.getX(), cursor.getY() - denominator.getHeight() + this.getFontHeight()/2 - FractionView.getSpace()));
			//canvas.fillText("-", cursor.getX(), cursor.getY() - denominator.getHeight() + this.getFontHeight()/2 - FractionView.getSpace());
		}
		
		
		this.numerator.render(canvas);
		this.denominator.render(canvas);
		
		Double lineY = cursor.getY() - denominator.getHeight() - FractionView.getSpace();
		
		if (isNegative()) {
			canvas.fillRectangle(cursor.getX() + minusWidth, lineY, rectangle.getWidth() - minusWidth, 2d);
		} else {
			canvas.fillRectangle(cursor.getX(), lineY, rectangle.getWidth(), 2d);
		}
	}
	

	/**
	 * set the position on canvas to render
	 * @param Cursor
	 */
	@Override
	public void setCursor(Cursor cursor) {
		cursor.setY(cursor.getY());
		
		Surface surface = new Surface();
		surface.setFont(this.font);
		Double minusWidth = 0d;
		if (this.isNegative()) minusWidth = this.minusSign.getWidth();
		
		
		super.setCursor(new Cursor(cursor.getX(), cursor.getY()));
		
		if (this.isNegative()) {
			cursor.setX(cursor.getX() + minusWidth);
		}
		//TODO center numerator and denominator add brackets
		numerator.setCursor(new Cursor(cursor.getX() + (this.getWidth() - minusWidth - numerator.getWidth())/2, cursor.getY() - denominator.getHeight() - 2*FractionView.getSpace()));
		
		denominator.setCursor(new Cursor(cursor.getX() + (this.getWidth() - minusWidth - denominator.getWidth())/2,  cursor.getY()));

		
	}
	
	/**
	 * @param font the font to set
	 */
	@Override
	public void setFont(String font) {
		this.numerator.setFont(font);
		this.denominator.setFont(font);
		

		if (font.indexOf("px")!=-1) {
			this.setFontHeight(new Double(font.substring(0, font.indexOf("px"))));
			this.font = font.substring(font.indexOf("px ")+3);
			this.minusSign.setFont(this.font, this.getFontHeight());
			
			update();
		} else {
			this.setFontHeight(null);
			this.font = null;
		}
	}
	
	/**
	 * @return the numerator
	 */
	public View getNumerator() {
		return numerator;
	}

	/**
	 * @return the denominator
	 */
	public View getDenominator() {
		return denominator;
	}

	/**
	 * updates the view after changes
	 */
	@Override
	public void update() {
		Surface surface = new Surface();
		surface.setFont(this.getFont());
		Double minusWidth = 0d;
		if (isNegative()) minusWidth = this.minusSign.getWidth();
		this.setWidth(Math.max(numerator.getWidth(), denominator.getWidth()) + minusWidth + 2*FractionView.space);
		this.setHeight(numerator.getHeight() + denominator.getHeight() + 2*FractionView.space);
		
	}
}
